package cloud.xlh.my_system.utils;

import cloud.xlh.my_system.pojo.Database;
import cloud.xlh.my_system.service.DatabaseService;
import com.alibaba.druid.pool.DruidPooledConnection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.SQLException;


/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : code-generator
 * @ Author: XuLeHuang
 * @ Date: 2022/5/31 23:45
 * @ Description:
 */
@Slf4j
@Component
public class ConnectionTestHelper {

    public boolean connect(Database database, boolean cache){
        DruidPooledConnection connection = null;
        try {
            connection = DynamicDataSource.getConnection(database, cache);
            return connection != null;
        }finally {
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    log.error("释放连接异常", e);
                }
            }else {
                if(cache && database.getId() != null){
                    //databaseService.lambdaUpdate().eq(Database::getId, database.getId()).set(Database::getStatus, 0).update();
                }
            }
        }
    }
}
